package com.hmdp;

import com.hmdp.entity.Shop;
import com.hmdp.service.IShopService;
import com.hmdp.utils.RedisConstants;
import org.mybatis.spring.annotation.MapperScan;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

@MapperScan("com.hmdp.mapper")
@SpringBootApplication
public class HmDianPingApplication implements CommandLineRunner {


    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IShopService shopService;

    public static void main(String[] args) {
        SpringApplication.run(HmDianPingApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // 创建布隆过滤器
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstants.BLOOMFITER_KEY);
        bloomFilter.tryInit(1000,0.03);
        List<Shop> list = shopService.list();
        // 将商铺id放入
        list.forEach(shop -> bloomFilter.add(shop.getId()));
        // 创建bitmap 将商铺id放入，并设置为1,不存在误判情况
        list.forEach(shop -> redisTemplate.opsForValue().setBit(RedisConstants.BITMAP_KEY, shop.getId(), true));
    }
}
